package com.example.newjiesai.controller;


import com.example.newjiesai.entity.Location;
import com.example.newjiesai.entity.Term;
import com.example.newjiesai.service.LocationService;
import com.example.newjiesai.service.TermService;
import com.example.newjiesai.util.LayuiData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class TermController {
    @Autowired
    private TermService terminfoService;
    @Autowired
    private LocationService locationService;
    public  String set;
    public String set()
    {
        return set;
    }
    public int termid;
    @RequestMapping("/getTerminfoList")
    @ResponseBody
    public LayuiData getTerminfoList (HttpServletRequest request){
        String term_imei = request.getParameter("term_imei");
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        if(page>=2){
            page = (page-1)*limit;
        }

        LayuiData layuiData = new LayuiData();

        if(term_imei==null)
        {
            List<Term> terminfoList = terminfoService.getTerminfoList( page, limit);
            int count = terminfoService.getTerminfoList2().size();
            layuiData.setCode(0);
            layuiData.setCount(count);
            layuiData.setMsg("数据请求成功");
            layuiData.setData(terminfoList);
            return layuiData;
        }
        else
        {  List<Term> termlist = new ArrayList();
            Term term=terminfoService.getTerminfoByImei(term_imei);
            if(term==null) {

                layuiData.setCode(0);
                layuiData.setCount(termlist.size());
                layuiData.setMsg("没有该设备");
                layuiData.setData(termlist);
                return layuiData;
            }
            else{

                termlist.add(term);
                layuiData.setCode(0);
                layuiData.setCount(termlist.size());
                layuiData.setMsg("数据请求成功");
                layuiData.setData(termlist);
                return  layuiData;



            }
        }
    }

    /**
     * 去新增用户界面
     * @return
     */
    @RequestMapping("/toTerminfo")
    public String toTerminfo (){

        return "terminfoAdd";
    }
    @RequestMapping("/List2")
    public String Terminfolist (Model model){
        List<Term> terminfoList = terminfoService.getTerminfoList(1,1);
        model.addAttribute("terminfoList",terminfoList);
        return "terminfoList";
    }








    @RequestMapping("/terminfoAdd")
    @Transactional
    @ResponseBody
    public Integer terminfoAdd (String term_imei,Integer term_provider){
        Term terminfo = new Term();
        Date day=new Date();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        terminfo.setterm_imei(term_imei);
        terminfo.setterm_provider(term_provider);
        int num = terminfoService.add(terminfo);
        return num;
    }

    @RequestMapping("/terminfodelete")
    @ResponseBody
    public Integer terminfodelete(Integer id) {

        int num = terminfoService.delete(id);

        return num;
    }
    @RequestMapping("/terminfoListpage")
    public String terminfoListpage() {

        return "terminfoListPage";
    }
    @RequestMapping("/List")
    public String list() {

        return "List";
    }



    /**
     * 根据id修改用户信息
     * @return
     */
    @RequestMapping("/terminfoUpdate1")
    @Transactional
    @ResponseBody
    public Integer terminfoUpdate (Integer id,Integer bind_type){
        Term terminfo = terminfoService.getTerminfoById(id);
        System.out.println(terminfo.getterm_imei());
        System.out.println(bind_type);
        if(bind_type==0) {
            System.out.println("update 0");
            terminfo.setif_chain(0);

            int num = terminfoService.update(terminfo);
            System.out.println(num);
            return num;
        }
        else
        {
            System.out.println("update 1");
            terminfo.setif_chain(1);

            int num = terminfoService.update(terminfo);
            System.out.println(num);
            return num;
        }




    }
    /**
     * 去查看界面
     * @param id

     * @return
     */
    @RequestMapping("/toLocation")
    public String terminfoDetail(Integer id) {

            //LeadcentMessage leadcentMessage=new LeadcentMessage();
           // leadcentMessage.setImei(terminfo.getterm_imei());
            //leadcentMessage.setType("1002");
            //leadcentMessage.setContent0("600");
           // model.addAttribute("leadcentMessage", leadcentMessage);
        termid=id;
        System.out.println(id);
            return "locationListPage";

    }
    @RequestMapping("/getLocationList")
    @ResponseBody
    public LayuiData getLocationList (HttpServletRequest request){


        String term_imei = terminfoService.getTerminfoById(termid).getterm_imei();
        System.out.println(term_imei);
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        if(page>=2){
            page = (page-1)*limit;
        }

        LayuiData layuiData = new LayuiData();
       List<Location> locationList =locationService.getLocationList(term_imei,page,limit);
        int count=locationService.getLocationcount(term_imei).size();
        layuiData.setCode(0);
        layuiData.setCount(count);
        layuiData.setMsg("数据请求成功");
        layuiData.setData(locationList);
        return layuiData;


    }





    /**
     * 去修改界面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/totermUpdate")
    public String totermUpdate(Integer id, Model model) {

        Term term = terminfoService.getTerminfoById(id);

       System.out.println("toupdate");
        model.addAttribute("term", term);
        // model.addAttribute("terminfo",terminfo);
        return "terminfoUpdate";

    }

    @RequestMapping("/terminfoUpdate")
    @Transactional
    @ResponseBody
    public int send1(Integer id,Integer if_chain,Integer upload_intervel)
    {   System.out.println(id+if_chain+upload_intervel);
        Term term= terminfoService.getTerminfoById(id);
        if(if_chain==1||if_chain==0)
        {
           term.setif_chain(if_chain);
        }
         if(upload_intervel<=60&&upload_intervel>0)
         {
             term.setupload_intervel(upload_intervel);
         }
         term.setif_upload(0);
            terminfoService.update(term);
          return 1;


    }












}
